package com.chuzhiyun.chengguoxy.feature.internship.clockin.Canendar;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.text.TextUtils;
import android.util.TypedValue;

import com.necer.calendar.ICalendar;
import com.necer.painter.CalendarPainter;
import com.necer.utils.CalendarUtil;

import org.joda.time.LocalDate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TicketPainter implements CalendarPainter {

    protected Paint mTextPaint;
    protected Paint mBgPaint;
    private final int mCircleRadius;
    private final Context mContext;
    private final ICalendar mICalendar;
    private final Map<LocalDate, String> mPriceMap;

    public TicketPainter(Context context, ICalendar iCalendar) {

        mContext = context;
        mICalendar = iCalendar;
        mTextPaint = getPaint();

        mBgPaint = getPaint();
        mBgPaint.setColor(Color.parseColor("#FF7A33"));
        mBgPaint.setStrokeWidth(3);
        mBgPaint.setAntiAlias(true);
        mBgPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mBgPaint.setTextAlign(Paint.Align.CENTER);

        mCircleRadius = DensityUtil.dp2px(context, 15);
        mPriceMap = new HashMap<>();
    }

    private Paint getPaint() {
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setTextAlign(Paint.Align.CENTER);
        return paint;
    }

    @Override
    public void onDrawToday(Canvas canvas, RectF rectF, LocalDate localDate, List<LocalDate> selectedDateList) {
        drawSelectBg(canvas, rectF, selectedDateList.contains(localDate), true);
        drawSolar(canvas, rectF, localDate, selectedDateList.contains(localDate), true);
        drawPrice(canvas, rectF, localDate, selectedDateList.contains(localDate), true);
    }

    @Override
    public void onDrawCurrentMonthOrWeek(Canvas canvas, RectF rectF, LocalDate localDate, List<LocalDate> selectedDateList) {
        drawSelectBg(canvas, rectF, selectedDateList.contains(localDate), true);
        drawSolar(canvas, rectF, localDate, selectedDateList.contains(localDate), true);
        drawPrice(canvas, rectF, localDate, selectedDateList.contains(localDate), true);
    }

    @Override
    public void onDrawLastOrNextMonth(Canvas canvas, RectF rectF, LocalDate localDate, List<LocalDate> selectedDateList) {
        drawSelectBg(canvas, rectF, selectedDateList.contains(localDate), false);
        drawSolar(canvas, rectF, localDate, selectedDateList.contains(localDate), false);
        drawPrice(canvas, rectF, localDate, selectedDateList.contains(localDate), false);
    }

    @Override
    public void onDrawDisableDate(Canvas canvas, RectF rectF, LocalDate localDate) {

    }

    //绘制选中背景
    private void drawSelectBg(Canvas canvas, RectF rectF, boolean isisSelected, boolean isCurrectMonthOrWeek) {
        if (isisSelected) {
            mBgPaint.setAlpha(isCurrectMonthOrWeek ? 255 : 1);
            canvas.drawCircle(rectF.centerX(), rectF.centerY(), mCircleRadius, mBgPaint);
        }
    }

    //绘制公历
    private void drawSolar(Canvas canvas, RectF rectF, LocalDate localDate, boolean isSelected, boolean isCurrectMonthOrWeek) {
        mTextPaint.setTextSize(DensityUtil.dp2px(mContext, 16));
        mTextPaint.setColor(0x2C2C2C);
        mTextPaint.setAlpha(isCurrectMonthOrWeek ? 255 : 1);
        if (CalendarUtil.isToday(localDate)) {  //当天
//            mTextPaint.setColor(0xFEFFFF);
            canvas.drawText("今", rectF.centerX(),  rectF.centerY()+16, mTextPaint);
        }
        else
        {
//            mTextPaint.setColor(0x2C2C2C);
            canvas.drawText(localDate.getDayOfMonth() + "", rectF.centerX(),  rectF.centerY()+16, mTextPaint);
        }
    }

    //绘制点
    private void drawPrice(Canvas canvas, RectF rectF, LocalDate localDate, boolean isSelected, boolean isCurrectMonthOrWeek) {
        String value = mPriceMap.get(localDate);

        if (!TextUtils.isEmpty(value)) {
            mTextPaint.setTextSize(DensityUtil.dp2px(mContext, 10));
            assert value != null;
            if (value.equals("normal")||value.equals("day-off"))
            {
                mTextPaint.setColor(Color.parseColor("#009FF0"));
            }
            if (value.equals("missing")||value.equals("warning"))
            {
                mTextPaint.setColor(Color.parseColor("#FF7A33"));
            }
            mTextPaint.setAlpha(255);
            mTextPaint.setAlpha(isCurrectMonthOrWeek ? 255 : 1);
            canvas.drawCircle(rectF.centerX(), rectF.centerY() + DensityUtil.dp2px(mContext, 20), 8, mTextPaint);
        }
    }

    public void setPriceMap(Map<LocalDate, String> priceMap) {
        if (priceMap != null) {
            mPriceMap.clear();
            mPriceMap.putAll(priceMap);
            mICalendar.notifyCalendar();
        }
    }
}

class DensityUtil {
    public static int dp2px(Context context, float dpVal) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                dpVal, context.getResources().getDisplayMetrics());
    }
}
